var echarts;
$(document).ready(function () {
    require.config({
        baseUrl: ctx + '/resource/plugins/echarts4',
        paths: {
            echarts: 'echarts',
            roma: 'theme/roma'
        }
    });
    loadDivByKpi();
});

//根据指标动态加载div
function loadDivByKpi(DATE_TYPE) {
    var divHtml =
        '<div class="tu-content" style="height: 480px">' +
            /*"资源总览"*/
        '<div class="test" style="margin-top:10px;margin-left:1.5%;width: 98%;height: 90%;" id="">' +
        '</div>';
    var list = kpiCode.split(',');
    for (var i = 0; i < list.length; i++) {
        var kpiIdG = list[i].replace(/-/g, '');//用作echarts的ID
        $('#itea').append(divHtml);
        $("div[class = 'test']:eq(" + i + ")").attr('id', kpiIdG);
        //调用echarts
        queryKpiContrastInfo(extUnitId, kpiIdG, list[i], timeHorizon);
    }

}
//折线图
function queryKpiContrastInfo(extUnitId, kpiIdG, kpiCode, timeHorizon) {
    require(
        [
            'echarts',
            'roma'
        ],
        function (ec) {
            echarts = ec;
            KpiContrastInfo = echarts.init(document.getElementById(kpiIdG));
            $.ajax({
                url: ctx + "/kpiContrast/queryKpiContrastInfo",
                type: "post",
                data: {extUnitId: extUnitId, kpiCode: kpiCode, timeHorizon: timeHorizon},
                success: function (data) {
                    var list = JSON.parse(data);
                    var ydata = [];
                    var avgValue = [];
                    var maxValue = [];
                    var mixValue = [];
                    var modelValue = [];
                    if (list != null && list.length != 0) {
                        for (var i = 0; i < list.length; i++) {
                            if (timeHorizon == "showOneWeek") {
                                x_value = list[i].DATA_TIME.split("-");
                                ydata.push(x_value[2]);
                            } else if (timeHorizon == "showOneMonth") {
                                x_value = list[i].DATA_TIME.split("-");
                                ydata.push(x_value[2]);
                            } else if (timeHorizon == "showOneYear") {
                                x_value = list[i].DATA_TIME.split("-");
                                ydata.push(x_value[1]);
                            } else {
                                x_value = list[i].DATA_TIME.replace(' ', '-').split("-");
                                ydata.push(x_value[3]);
                            }
                            avgValue.push(list[i].AVG_VALUE);
                            maxValue.push(list[i].MAX_VALUE);
                            mixValue.push(list[i].MIN_VALUE);
                            modelValue.push(list[i].MODEL_VALUE);
                        }
                        option = {
                            tooltip: {
                                trigger: 'axis'
                            },
                            legend: {
                                data: ['平均值', '最大值', '最小值', '众数']
                            },
                            grid: {
                                left: '3%',
                                right: '4%',
                                bottom: '3%',
                                containLabel: true
                            },
                            xAxis: {
                                type: 'category',
                                boundaryGap: false,
                                data: ydata,
                                axisLabel: {
                                    interval: 0,
                                    rotate: 35,
                                    formatter: function (value) {
                                        /*debugger*/
                                        var ret = "";//拼接加\n返回的类目项
                                        var maxLength = 15;//每项显示文字个数
                                        var valLength = value.length;//X轴类目项的文字个数
                                        var rowN = Math.ceil(valLength / maxLength); //类目项需要换行的行数
                                        if (rowN > 1)//如果类目项的文字大于3,
                                        {
                                            for (var i = 0; i < rowN; i++) {
                                                var temp = "";//每次截取的字符串
                                                var start = i * maxLength;//开始截取的位置
                                                var end = start + maxLength;//结束截取的位置
                                                //这里也可以加一个是否是最后一行的判断，但是不加也没有影响，那就不加吧
                                                temp = value.substring(start, end) + "\n";
                                                ret += temp; //凭借最终的字符串
                                            }
                                            return ret;
                                        }
                                        else {
                                            return value;
                                        }
                                    }
                                }
                            },
                            yAxis: {
                                type: 'value'
                            },
                            series: [
                                {
                                    name: '平均值',
                                    type: 'line',
                                    stack: '总量',
                                    data: avgValue
                                },
                                {
                                    name: '最大值',
                                    type: 'line',
                                    stack: '总量',
                                    data: maxValue
                                },
                                {
                                    name: '最小值',
                                    type: 'line',
                                    stack: '总量',
                                    data: mixValue
                                },
                                {
                                    name: '众数',
                                    type: 'line',
                                    stack: '总量',
                                    data: modelValue
                                }
                            ]
                        };
                        KpiContrastInfo = echarts.init(document.getElementById(kpiIdG));
                        KpiContrastInfo.setOption(option);
                        $('#' + kpiIdG).before(list[0].KPI_NAME);
                    } else {
                        $('#' + kpiIdG).parent().remove();
                    }
                },
                error: function (textStatus) {
                    console.log("error:" + textStatus.status);
                }
            });
        }
    );
}


